package com.dimong.www.loan.ui.frag.main;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.loadmore.SwipeRefreshHelper;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.dimong.loan.sdk.net.NetHttpCallback;
import com.dimong.loan.sdk.ui.adapter.BaseRecyclerAdapter;
import com.dimong.loan.sdk.ui.frag.BaseFragment;
import com.dimong.www.loan.R;
import com.dimong.www.loan.bean.MainBean;
import com.dimong.www.loan.bean.ProdBean;
import com.dimong.www.loan.net.LoadHttpUtil;
import com.dimong.www.loan.net.response.prod.GetProdList;
import com.dimong.www.loan.ui.UIHelper;
import com.dimong.www.loan.ui.adapter.main.MainAdapter;
import com.dimong.www.loan.ui.base.BaseRefreshFragment;

public class ProdListFrag extends BaseRefreshFragment {
    private SwipeRefreshLayout srl_frag_main_one;
    private RecyclerView rv_frag_main_one;
    private MainAdapter mainAdapter;
    private SwipeRefreshHelper mSwipeRefreshHelper;
    private int page = 1;
    private String classify_id;

    @Override
    public void beforeInitView() {

    }

    @Override
    public void initView() {
        super.initView();
        srl_frag_main_one = (SwipeRefreshLayout) findViewById(R.id.srl_frag_main_one);
        rv_frag_main_one = (RecyclerView) findViewById(R.id.rv_frag_main_one);
        rv_frag_main_one.setItemAnimator(null);
    }

    @Override
    public void setViewStatus() {
        if (getArguments() != null) {
            classify_id = getArguments().getString("classify_id");
        }
        rv_frag_main_one.setLayoutManager(new LinearLayoutManager(getActivity()));
        mainAdapter = new MainAdapter();
        rv_frag_main_one.setAdapter(new RecyclerAdapterWithHF(mainAdapter));

        mSwipeRefreshHelper = new SwipeRefreshHelper(srl_frag_main_one, rv_frag_main_one);
        mSwipeRefreshHelper.setLoadMoreEnable(true);
        mSwipeRefreshHelper.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                page++;
                getData();
            }
        });
        mSwipeRefreshHelper.setOnSwipeRefreshListener(new SwipeRefreshHelper.OnSwipeRefreshListener() {
            @Override
            public void onfresh() {
                page = 1;
                getData();
            }
        });

        mainAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                MainBean mainBean = (MainBean) mainAdapter.getItem(position);
                if (mainBean.getObject() instanceof ProdBean) {
                    ProdBean prodBean = (ProdBean) mainBean.getObject();
                    UIHelper.gotoProdDetailed(getActivity(), prodBean.getProd_id());
                }
            }
        });
        srl_frag_main_one.setVisibility(View.GONE);
        showRefreshLoading();
        page = 1;
        getData();
    }

    @Override
    public int getViewResourceId() {
        return R.layout.frag_main_one;
    }

    public void getData() {
        LoadHttpUtil.getProdList(page, classify_id, new NetHttpCallback() {
            @Override
            public void onResult(String result) {
                mSwipeRefreshHelper.refreshComplete();
                super.onResult(result);
                showRefreshSuccess();
                srl_frag_main_one.setVisibility(View.VISIBLE);
                GetProdList getProdList = gson.fromJson(result, GetProdList.class);
                setData(getProdList);
            }

            @Override
            public void onError(String errorCode, String error) {
                super.onError(errorCode, error);
                if (mainAdapter.getItemCount() == 0) {
                    showRefreshError();
                }
                mSwipeRefreshHelper.refreshComplete();
                showToast(error);
            }
        });
    }

    private void setData(GetProdList getProdList) {
        if (page == 1) {
            mainAdapter.clear();
        }

        if (getProdList.getList() != null && getProdList.getList().size() != 0) {
            mSwipeRefreshHelper.loadMoreComplete(true);
            mSwipeRefreshHelper.setLoadMoreEnable(true);
            for (int i = 0; i < getProdList.getList().size(); i++) {
                MainBean bean1 = new MainBean();
                bean1.setType(MainAdapter.ITEM_HOT_RECOMMEND);
                bean1.setObject(getProdList.getList().get(i));
                mainAdapter.addData(bean1);
            }
        } else {
            mSwipeRefreshHelper.setLoadMoreEnable(false);
            mSwipeRefreshHelper.loadMoreComplete(false);
        }

        mainAdapter.updateChange();
    }

    @Override
    public RelativeLayout getContainView() {
        return (RelativeLayout) findViewById(R.id.rl_frag_main_one);
    }
}
